#include "tss.h"
#include "stdint.h"
#include "global.h"
#include "string.h"
#include "print.h"

//定义tss的数据结构，在内存中tss的分布就是这个结构体
struct tss {
    uint32_t backlink;
    uint32_t* esp0;
    uint32_t ss0;
    uint32_t* esp1;
    uint32_t ss1;
    uint32_t* esp2;
    uint32_t ss2;
    uint32_t cr3;
    uint32_t (*eip) (void);
    uint32_t eflags;
    uint32_t eax;
    uint32_t ecx;
    uint32_t edx;
    uint32_t ebx;
    uint32_t esp;
    uint32_t ebp;
    uint32_t esi;
    uint32_t edi;
    uint32_t es;
    uint32_t cs;
    uint32_t ss;
    uint32_t ds;
    uint32_t fs;
    uint32_t gs;
    uint32_t ldt;
    uint16_t trace;
    uint16_t io_base;
};

struct tss* tss;

//用于更新TSS中的esp0的值，让它指向线程/进程的0级栈
void update_tss_esp(struct task_struct* pthread) {
    tss = (struct tss*)(0xc0000b0b);
    tss->esp0 = (uint32_t*)((uint32_t)pthread + PG_SIZE);
}
void tss_init(void)
{
    put_str("tss_init start\n");
    tss = (struct tss*)(0xc0000b0b);
    memset(((void *)(0xc0000b0b + 0x80)), 0xff, 0x400);
    tss->io_base = 0x80;
    put_str("tss_init and ltr done\n");
}